package db_main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func GetVisitorLogCount(param map[string]interface{}) (int64, error) {
	fName := "GetVisitorLogCount"
	var o int64
	table := mysql.GetRTable(&models_main.VisitorLog{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Count(&o).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, err
}

func InsertVisitorLog(visitorLog *models_main.VisitorLog) error {
	fName := "InsertVisitorLog"
	err := global.GetX().Create(visitorLog).Error
	if err != nil {
		return fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return nil
}
